import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang';

/**
 * 保存物料
 * @param callback 接口回调
 * @returns
 */
const addAssetMaterielService = (callback: (res: any, clickType: boolean) => void) => {
  const addAssetMateriel = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        // state: `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        // ownerID: getUserID(),
        // ownPositionID: getPosIDs(),
        // ownDepartmentID: getUserDepID(),
        // ownCompanyID: getCompanyID(),
        // dutyerID: getUserID(),
        // dutyPositionID: getPosIDs(),
        // dutyDepartmentID: getUserDepID(),
        // dutyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/assetMaterial/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    addAssetMaterielRun: addAssetMateriel?.run,
    addAssetMaterielLoading: addAssetMateriel?.loading,
  };
};

/**
 * 编辑物料
 * @param callback 接口回调
 * @returns
 */
const editAssetMaterielService = (callback: (res: any, clickType: boolean) => void) => {
  const editAssetMateriel = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/assetMaterial/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    editAssetMaterielRun: editAssetMateriel?.run,
    editAssetMaterielLoading: editAssetMateriel?.loading,
  };
};

/**
 * 作废物料
 * @param callback 接口回调
 * @returns
 */
const cancelAssetMaterielService = (callback: (res: any, clickType: boolean) => void) => {
  const cancelAssetMateriel = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/assetMaterial/cancel`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    cancelAssetMaterielRun: cancelAssetMateriel?.run,
    cancelAssetMaterielLoading: cancelAssetMateriel?.loading,
  };
};

export { addAssetMaterielService, editAssetMaterielService, cancelAssetMaterielService };
